//
//  ViewController.m
//  CameraDemo
//
//  Created by sanfang on 2017/1/3.
//  Copyright © 2017年 ziz. All rights reserved.
//

#import "ViewController.h"
#import <Foundation/Foundation.h>

////不带界面的语音识别控件
#import "iflyMSC/IFLYSpeechRecognizerDelegate.h"
#import "iflyMSC/IFLYSpeechRecognizer.h"
#import "iflyMSC/IFlySpeechConstant.h"
#import "ISRDataHelper.h"

#import "XXConstants.h"

#import "XXCameraTool.h"


@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,IFlySpeechRecognizerDelegate>
{
    BOOL isTorchOn;
    NSInteger flashModeNum;
    
}

//@property (nonatomic, strong)IFlyRecognizerView *iflyRecognizerView;
@property (nonatomic, strong)IFlySpeechRecognizer *speechRecognizer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isTorchOn = NO;
    flashModeNum = 0;
    
    [[XXCameraTool shareInstance]enterCameraInSuperView:self.view];
    
    IFlySpeechRecognizer *_iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
    [_iFlySpeechRecognizer setParameter:@"400000" forKey:[IFlySpeechConstant VAD_EOS]];

    //启动识别服务
    self.speechRecognizer = [IFlySpeechRecognizer sharedInstance];
    self.speechRecognizer.delegate = self;
    [self.speechRecognizer startListening];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[IFlySpeechRecognizer sharedInstance]stopListening];
    [[IFlySpeechRecognizer sharedInstance]cancel];

}


-(void)dealloc
{
    [[IFlySpeechRecognizer sharedInstance]stopListening];
    [[IFlySpeechRecognizer sharedInstance]cancel];
}
/**
 进入相册
 */
-(void)enterPhotoAlbum
{
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
    imgPicker.delegate = self;
    [self presentViewController:imgPicker animated:YES completion:nil];
}

/**
 判断一个字符串是否包含数组内字符串

 @param checkStr 被检测字符串
 @param strings 包含的字符串数组
 @return 是否包含数组内字符串
 */
-(BOOL)checkString:(NSString *)checkStr containStrings:(NSArray *)strings
{
    for (NSString *aStr in strings)
    {
        if ([checkStr containsString:aStr])
        {
            return YES;
        }
    }
    return NO;
}


/**
 返回首页

 @param sender 首页按钮
 */
- (IBAction)goHome:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



/**
 打开声音控制

 @param sender 声控按钮
 */
- (IBAction)openVoiceControl:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    if (btn.selected)
    {
        [[IFlySpeechRecognizer sharedInstance]startListening];
    }
    else
    {
        [[IFlySpeechRecognizer sharedInstance]stopListening];
    }
}

/**
 进入相册

 @param sender 相册按钮
 */
- (IBAction)enterAlbum:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.layer.cornerRadius = btn.bounds.size.width/2;
    [self enterPhotoAlbum];
}

/**
 切换前后摄像头

 @param sender 切换按钮
 */
- (IBAction)switchCamera:(id)sender {
    [[XXCameraTool shareInstance] toggleCamera];
}


/**
 拍照

 @param sender 拍照按钮
 */
- (IBAction)takePhoto:(id)sender {
    [[XXCameraTool shareInstance] takePhoto];
}

/**
 开启闪光灯

 @param sender 闪光灯按钮
 */
- (IBAction)changeFlash:(id)sender {
    UIButton *btn = (UIButton *)sender;
    
    
    AVCaptureFlashMode mode;
    flashModeNum ++;
    if (flashModeNum > 2)
    {
        flashModeNum = 0;
    }
    NSString *flashStr = @"flashing_auto";
    if (flashModeNum == 0) {
        mode = AVCaptureFlashModeOff;
        flashStr = @"flashing_off";
    }
    else if (flashModeNum == 1)
    {
        mode = AVCaptureFlashModeOn;
        flashStr = @"flashing_on";
    }
    else
    {
        mode = AVCaptureFlashModeAuto;
        flashStr = @"flashing_auto";
    }
    UIImage *flashImg = [UIImage imageNamed:flashStr];
    [btn setImage:flashImg forState:UIControlStateNormal];
    [[XXCameraTool shareInstance] setFlashMode:mode];
}

/**
 开启手电筒

 @param sender 手电筒按钮
 */
- (IBAction)openTorch:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    btn.layer.cornerRadius = 30;
    XXLog(@"radius = %f", btn.frame.size.width/2);
    isTorchOn = !isTorchOn;
    btn.selected = isTorchOn;
    [[XXCameraTool shareInstance]setTorchOn:isTorchOn];

}


#pragma mark === lazyLoad ===
#pragma mark === UIImagePickerDelegate ===

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
    XXLog(@"edit \n%@", editingInfo);
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    XXLog(@"edit \n%@", info);
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark === IFlyDelegate ===
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast;
//- (void)onResult:(NSArray *)resultArray isLast:(BOOL) isLast
{
    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = results[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    NSString * resultFromJson =  [ISRDataHelper stringFromJson:resultString];
    NSLog(@"resultFromJson=%@",resultFromJson);
    XXCameraTool *cameraTool = [XXCameraTool shareInstance];
    NSArray *takePhotoStrings = @[@"拍" ,@"太", @"拍照", @"够",@"夜", @"茄子",@"美", @"每"];
    NSArray *albumStrings = @[@"相册"];
    NSArray *qieHuanStrings = @[@"切换", @"摄像头"];
    BOOL takePhoto = [self checkString:resultFromJson containStrings:takePhotoStrings];
    BOOL alblum = [self checkString:resultFromJson containStrings:albumStrings];
    BOOL qieHuan = [self checkString:resultFromJson containStrings:qieHuanStrings];
    
    if (takePhoto)
    {
        [[XXCameraTool shareInstance] takePhoto];
    }
    else if (alblum)
    {
        [self enterPhotoAlbum];
    }
    else if (qieHuan)
    {
        [cameraTool toggleCamera];
    }
    else
    {
        
    }
}

/*!
 *  识别结束回调
 *
 *  @param error 识别结束错误码
 */
- (void)onError: (IFlySpeechError *) error
{
    
}


#pragma mark === UITouch ===

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    [[XXCameraTool shareInstance]focusCursorInPoint:point];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
